package com.example.spider.config.support;

import com.example.spider.config.support.annotation.SpiderExHandler;
import com.example.spider.config.support.annotation.SpiderHandlerAdvice;
import com.example.spider.config.support.annotation.SpiderResultExHandler;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.ReflectionUtils.MethodFilter;

import java.lang.annotation.Annotation;
import java.util.function.Function;

/**
 * AOP 的扫描注解
 */
public enum ExceptionAdvanceEnum {

    SPIDER_ANNO(SpiderResultExHandler.class, SpiderResultExHandler::value),
    HANDLER_ANNO(SpiderExHandler.class, SpiderExHandler::value),

    ;
    final Class<? extends Annotation>                                  annotationClass;
    final Class<? extends Annotation>                                  adviceClass = SpiderHandlerAdvice.class;
    final MethodFilter                                                 methodFilter;
    final Function<Annotation, Class<? extends Throwable>[]> supportExClassGetter;

    private <T extends Annotation> ExceptionAdvanceEnum(Class<T> annotationClass,
                                 Function<T, Class<? extends Throwable>[]> supportExClassGetter) {
        this.annotationClass = annotationClass;
        this.methodFilter = method -> AnnotatedElementUtils.hasAnnotation(method, annotationClass);
        this.supportExClassGetter = (Function<Annotation, Class<? extends Throwable>[]>) supportExClassGetter;
    }

    /**
     * Getter method for property <tt>annotationClass</tt>.
     *
     * @return property value of annotationClass
     */
    public Class<? extends Annotation> getAnnotationClass() {
        return annotationClass;
    }

    /**
     * Getter method for property <tt>methodFilter</tt>.
     *
     * @return property value of methodFilter
     */
    public MethodFilter getMethodFilter() {
        return methodFilter;
    }

    /**
     * Getter method for property <tt>supportExClassGetter</tt>.
     *
     * @return property value of supportExClassGetter
     */
    public Function<Annotation, Class<? extends Throwable>[]> getSupportExClassGetter() {
        return supportExClassGetter;
    }
}